function TS = buildTS(data, header, dates, type)
% BuildTS(data, header, dates, type) Build a TS structure
% If data is a TS structure, it uses it as a template makes data all NaNs
% If type is '3D', builds a 3D TS structure (e.g. covm)
% If type is 'rownames' builds a TS structure with rownames rather than 
%   dates.  The dates input argument should be a cell array of rownames.
%

if nargin < 4, type = ''; end

if isstruct(data)
    TS.data = zeros(size(data.data)) + NaN;
    TS.header = data.header;
    if isfield(data, 'dates')
      TS.dates = data.dates;
    end
    if isfield(data, 'rownames')
      TS.rownames = data.rownames;
    end
else
    if isempty(data)
        na = length(header);
        N = length(dates);
        if strcmp(type, '3D')
           TS.data = zeros(na,na,N) + NaN;
        else
           TS.data = zeros(N,na) + NaN;
        end
    else
        TS.data = data;
    end
    TS.header = header;
    if ~strcmp(type, 'rownames') 
        TS.dates = dates;
    else
        TS.rownames = dates;
    end
end
return